package com.dao;

import com.DBConfig.DBUtil;
import com.pojo.Record;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RecordDao {
    private static Connection conn;

    static {
        conn = DBUtil.getConn();
    }

    public static List<Record> getRecordBySql(String sql) {
        Statement stmt = null;
        ResultSet result = null;
        List<Record> records = new ArrayList<>();
        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sql);
            while (result.next()) {
                Record record = new Record();
                record.setId(result.getString("id"));
                record.setCar_id(result.getString("car_id"));
                record.setUser_id(result.getString("user_id"));
                record.setRentTime(result.getTimestamp("rentTime"));
                record.setBackTime(result.getTimestamp("backTime"));
                record.setPrice(result.getBigDecimal("price"));
                records.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (result != null) result.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return records;
    }

    public static boolean addRecord(Record record) {
        String sql = "insert into record(id,user_id,car_id,rentTime) values (?,?,?,?)";
        PreparedStatement ptst = null;
        try {
            ptst = conn.prepareStatement(sql);
            ptst.setString(1, record.getId());
            ptst.setString(2, record.getUser_id());
            ptst.setString(3, record.getCar_id());
            ptst.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
            ptst.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ptst != null) ptst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static Record getRecordByid(String id) {
        String sql = "select id,user_id,car_id,rentTime,backTime,price from record where id=?";
        PreparedStatement ptmt = null;
        Record record = null;
        ResultSet result = null;

        try {
            ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, id);
            result = ptmt.executeQuery();
            while (result.next()) {
                record = new Record(result.getString("id"),
                        result.getString("user_id"),
                        result.getString("car_id"),
                        result.getTimestamp("rentTime"),
                        result.getTimestamp("backTime"),
                        result.getBigDecimal("price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (result != null) result.close();
                if (ptmt != null) ptmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return record;
    }

    public static void setRecord(String id, BigDecimal price) {
        String sql = "UPDATE record SET backTime = now(),price=(TIMESTAMPDIFF(HOUR,rentTime,NOW())/24+1)*? WHERE record.id=?";

        PreparedStatement ptmt = null;
        try {
            ptmt = conn.prepareStatement(sql);
            ptmt.setBigDecimal(1,price);
            ptmt.setString(2, id);
            ptmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ptmt != null) ptmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
